//
//  Toast.h
//  YanXiuWang
//
//  Created by 招利 李 on 16/11/18.
//  Copyright © 2016年 小分队. All rights reserved.
//

#import <UIKit/UIKit.h>

// Toast 便捷调用
#define Toast [ToastManager shared]

// Toast 通知的视图间距
#define Toast_Interval 5.0
// Toast 通知默认字体大小
#define Toast_FontSize 16.0


@class ToastTask, ToastModal;

typedef void (^TOAST_NEED_LAYOUT)(ToastTask * task);
typedef ToastTask * (^TOAST_TASK)(TOAST_NEED_LAYOUT layoutBlock);
typedef ToastTask * (^TOAST_CALL)(dispatch_block_t block);
typedef ToastTask * (^TOAST_EDGE)(UIEdgeInsets insets);
typedef ToastTask * (^TOAST_TIME)(NSTimeInterval second);
typedef ToastTask * (^TOAST_MASK)(UIViewAutoresizing mask);

typedef ToastTask * (^TOAST_TEXT)(NSString * text);
typedef ToastTask * (^TOAST_ATTR)(NSAttributedString * attr);
typedef ToastTask * (^TOAST_VIEW)(UIView * view);
typedef ToastTask * (^TOAST_CTLR)(UIViewController * controller);


#pragma mark - Toast 管理中心
@interface ToastManager : NSObject

@property (nonatomic, readonly) UIWindow *overlayWindow;

/** 模态通知任务队列 遮挡并阻止用户操作 */
@property (nonatomic, readonly) ToastTask  * currentToast;
/** 模态通知任务队列 遮挡并阻止用户操作 */
@property (nonatomic, readonly) ToastModal * currentModal;

@property (nonatomic, readonly) NSTimeInterval minDismissAfter;
/** 模态通知任务队列 遮挡并阻止用户操作 */
@property (nonatomic, readonly) NSMutableArray<__kindof ToastTask *> * modalQueue;
/** 手动通知任务队列 需要自行调用 dismiss方法 */
@property (nonatomic, readonly) NSMutableArray<__kindof ToastTask *> * stateQueue;
/** 自动通知任务队列 超时自动调用 dismiss方法 */
@property (nonatomic, readonly) NSMutableArray<__kindof ToastTask *> * toastQueue;
/** 通知停止任务队列 用于做移除动画 */
@property (nonatomic, readonly) NSMutableArray<__kindof ToastTask *> * cleanQueue;

/** 弹出视图控制器 作为 Toast  */
@property (nonatomic, readonly) TOAST_CTLR makeController;
/** 弹出活动状态 作为 Toast  */
@property (nonatomic, readonly) TOAST_TEXT makeLoading;
/** 弹出视图 作为 Toast  */
@property (nonatomic, readonly) TOAST_VIEW makeView;
/** 弹出文字 作为 Toast  */
@property (nonatomic, readonly) TOAST_TEXT makeText;
/** 弹出富文本 作为 Toast  */
@property (nonatomic, readonly) TOAST_ATTR makeAttr;


@end

#pragma mark - Toast 通知任务
@class ToastModal;
@interface ToastTask : NSObject

@property (nonatomic, readonly) UIViewController * childController;
@property (nonatomic, readonly) UIView * containerView;
@property (nonatomic, readonly) UIView * view;
@property (nonatomic, readonly) BOOL visable;

/** 将通知视图 添加到 将要显示队列 onDismiss中需使用 `__weak typeof(self) weakSelf = self;` */
- (instancetype)showOnDismiss:(dispatch_block_t)onDismiss;

/** 立即隐藏此 Toast  */
- (void)dismiss;
/** 在下一轮动画中隐藏 Toast  */
- (void)dismissLater;

/** Toast 视图添加到容器时的边距  */
@property (nonatomic, readonly) TOAST_EDGE layoutInsets;
/** Toast 视图添加到容器时的边距  */
@property (nonatomic, readonly) TOAST_MASK autoresizingMask;
/** block 中使用 weakSelf 避免内存泄露  */
@property (nonatomic, readonly) TOAST_CALL showAnimations;
@property (nonatomic, readonly) TOAST_CALL hideAnimations;
/** 指定 Toast 视图容器  */
@property (nonatomic, readonly) TOAST_VIEW backgroundView;
/** 指定当 controller viewDidDisappear 时 hide ToastTask  */
@property (nonatomic, readonly) TOAST_CTLR dismissWhenDidDisappear;
/** 指定显示几秒后自动关闭 */
@property (nonatomic, readonly) TOAST_TIME autoDismissLaterThan;
/** 如需布局开始回调 */
@property (nonatomic, readonly) TOAST_TASK layoutSubViews;

/** 如需布局开始回调 */
@property (nonatomic, readonly) ToastModal *hasOverlay;

@end

#pragma mask - Modal Overlay Toast
@interface ToastModal : ToastTask

/** 当用户点击覆盖层时 自动退出 */
@property (nonatomic, readonly) ToastModal * dismissWhenOverlayTapped;

@end

#pragma mark - Toast 随 ViewController DidDisappear 一起消失的勾子
@interface UIViewController (ToastDisappear)

- (void)_selfDidDisappear:(BOOL)animated;

@end

#pragma mark - 单例相关
@interface ToastManager (Once)

+ (instancetype)shared;

@end
